package com.zyj.eps.controller;

import com.alibaba.fastjson.JSON;
import com.zyj.eps.pojo.Goods;
import com.zyj.eps.pojo.Record;
import com.zyj.eps.pojo.User;
import com.zyj.eps.pojo.Utils;
import com.zyj.eps.service.GoodsServiceImpl;
import com.zyj.eps.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author super
 * @date 2022/4/2 13:38
 */

@Controller
@RestController
@CrossOrigin
@SuppressWarnings("all")//抑制警告
public class GoodsController {
    @Autowired
    private GoodsServiceImpl goodsService;
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/getAllGoods")
    public String getAllGoods() {
        List<Goods> allGoods = goodsService.getAllGoods();
        List<Integer> allNum = goodsService.getAllNum();
        HashMap<String, Object> map = new HashMap<>();
        map.put("allGoods",allGoods);
        map.put("allNum",allNum);
        return JSON.toJSONString(map);
    }
    @RequestMapping("applyGoods")
    public String applyGoods(@RequestBody Utils utils) {
        User userByUUID = userService.getUserByUUID(utils.getUserID());
        Goods goodsByName = goodsService.getGoodsByName(utils.getGoodsname());
        goodsByName.setNum(goodsByName.getNum()-utils.getNum());
        int i = goodsService.updateGoods(goodsByName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        goodsService.insertGoodsApplyInfo(userByUUID.getUid(),userByUUID.getUname(),utils.getGoodsname(),sdf.format(new Date()),utils.getNum());
        return JSON.toJSONString("ok");
    }
    @RequestMapping("getAllRecordById")
    public String getAllRecordById(int uid) {
        List<Record> allRecordById = goodsService.getAllRecordById(uid);
        Collections.reverse(allRecordById); //实现list集合逆序排列
        return JSON.toJSONString(allRecordById);
    }
}
